package com.knowledge.graph.service.impl;


import com.knowledge.graph.mapper.CourseMapper;
import com.knowledge.graph.mapper.NodeAndVideoMapper;
import com.knowledge.graph.mapper.VideoAndCourseMapper;
import com.knowledge.graph.mapper.VideoMapper;
import com.knowledge.graph.model.entity.Course;
import com.knowledge.graph.model.entity.NodeAndVideo;
import com.knowledge.graph.model.entity.Video;
import com.knowledge.graph.model.entity.VideoAndCourse;
import com.knowledge.graph.service.VideoService;
import com.knowledge.graph.model.vo.ResponseVO;
import com.knowledge.graph.model.vo.StatusCode;
import com.knowledge.graph.model.vo.VideoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.knowledge.graph.utils.FileUtils;

/**
 * @author jiwenlong
 * @version 1.0
 * @description: VideoService的实现类
 * @date 2023/11/7 15:07
 */
@Slf4j
@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private VideoAndCourseMapper videoAndCourseMapper;

    @Autowired
    private NodeAndVideoMapper nodeAndVideoMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public ResponseVO addVideo(MultipartFile video, Integer courseID, List<Integer> nodeIDs) {
        Course course = courseMapper.selectById(courseID);
        File videoFile = FileUtils.addFile(course.getCourseAddress(), video);
        Video videoPO = new Video(videoFile.getName(),videoFile.getAbsolutePath(),null);
        videoMapper.insert(videoPO);
        videoAndCourseMapper.insert(new VideoAndCourse(course.getId(),videoPO.getId()));
        for (Integer nodeID:nodeIDs) {
            nodeAndVideoMapper.insert(new NodeAndVideo(videoPO.getId(),nodeID));
        }
        return new ResponseVO(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),videoPO);
    }

    @Override
    public ResponseVO deleteVideo(Integer videoID) {
        Video video = videoMapper.selectById(videoID);
        String videoAddress = video.getVideoAddress();
        File videoFile = new File(videoAddress);
        FileUtils.deleteFile(videoFile);
        videoMapper.deleteById(videoID);
        videoAndCourseMapper.deleteByVideoID(videoID);
        nodeAndVideoMapper.deleteByVideoID(videoID);
        return new ResponseVO(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
    }

    @Override
    public ResponseVO update(Integer videoID, String videoName) {
        Video video = videoMapper.selectById(videoID);
        Integer courseID = videoAndCourseMapper.selectByVideoID(videoID);
        Course course = courseMapper.selectById(courseID);
        String videoAddress = video.getVideoAddress();
        String[] split = videoAddress.split("\\.");
        String fileLastName = split[1];
        File videoFile = new File(videoAddress);
        File newVideoFile = new File(course.getCourseAddress(), videoName + "." + fileLastName);
        videoFile.renameTo(newVideoFile);
        video.setVideoName(videoName);
        video.setVideoAddress(newVideoFile.getAbsolutePath());
        videoMapper.updateById(video);
        return new ResponseVO(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),video);
    }

    @Override
    public ResponseVO getVideo(Integer courseID) {
        List<Integer> videoIDs = videoAndCourseMapper.selectByCourseId(courseID);
        List<VideoVO> videoVOS = new ArrayList<>();
        for (Integer videoID:videoIDs) {
            Video video = videoMapper.selectById(videoID);
            videoVOS.add(new VideoVO(videoID,video.getVideoName()));
        }
        return new ResponseVO(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),videoVOS);
    }

    @Override
    public ResponseVO getVideoByID(Integer videoID) {
        Video video = videoMapper.selectById(videoID);
        return new ResponseVO(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),video);
    }


}
